home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 03 / wintro6 / font.c < prev    next >
C/C++ Source or Header  |  1991-05-01  |  8KB  |  276 lines

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <memory.h>
  4. #include <windows.h>
  5. #include "stock.h"
  6.  
  7. #define ENUMFACES  0 
  8. #define ENUMSIZES  1 
  9.  
  10. #define MAXFONT   64
  11.  
  12. typedef struct tagFontInfo
  13. {
  14.   char szTypeface[64];
  15.   BYTE lfPitchAndFamily;
  16.   BYTE lfCharSet;
  17. } FONTINFO;
  18.  
  19. FONTINFO FontInfo[MAXFONT];
  20.  
  21.  
  22. static int   idxTypeface; 
  23. static BYTE  abFontSizeBitmask[256/8 + sizeof(WORD)];   // room for 256 different sizes
  24.  
  25.  
  26. VOID PASCAL GetTypefaces(HWND hWnd);
  27. VOID PASCAL GetFontSizes(HWND hWnd, int idxFont);
  28. int FAR PASCAL EnumFunc(LPLOGFONT lpLogFont, LPTEXTMETRIC lpTextMetric, 
  29.                 short FontType, LPSTR lpData);
  30. VOID PASCAL ShowSampleFont(HWND hDlg, BOOL bPermanent);
  31. int BitMaskSet(BYTE *map, WORD c, WORD val);
  32. int BitMaskTest(BYTE *map, WORD c);
  33.  
  34.  
  35. BOOL FAR PASCAL FontDlgProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  36. {
  37.   int  i;
  38.   char szBuf[128];
  39.  
  40.   switch (message)
  41.   {
  42.     case WM_INITDIALOG :
  43.       if (!hCurrStockInfo)
  44.       {
  45.         EndDialog(hDlg, FALSE);
  46.         return TRUE;
  47.       }
  48.  
  49.       GetTypefaces(hDlg);
  50.       for (i = 0;  i < idxTypeface;  i++)
  51.       {
  52.         SendDlgItemMessage(hDlg, ID_FACELIST, LB_ADDSTRING, 0,
  53.                            (LONG) (LPSTR) FontInfo[i].szTypeface);
  54.       }
  55.       SendDlgItemMessage(hDlg, ID_FACELIST, LB_SETCURSEL, 0, 0L);
  56.  
  57.       GetFontSizes(hDlg, 0);
  58.       for (i = 1;  i < 256;  i++)
  59.       {
  60.         if (BitMaskTest(abFontSizeBitmask, i))
  61.         {
  62.           sprintf(szBuf, "%d", i);
  63.           SendDlgItemMessage(hDlg, ID_SIZELIST, LB_ADDSTRING, 0,
  64.                                     (LONG) (LPSTR) szBuf);
  65.         }
  66.       }
  67.       SendDlgItemMessage(hDlg, ID_SIZELIST, LB_SETCURSEL, 0, 0L);
  68.       ShowSampleFont(hDlg, FALSE);
  69.       return TRUE;
  70.  
  71.  
  72.     case WM_COMMAND :
  73.       switch (wParam)
  74.       {
  75.         case ID_FACELIST :
  76.           switch (HIWORD(lParam))
  77.           {
  78.             case LBN_SELCHANGE :
  79.               i = (int) SendDlgItemMessage(hDlg,ID_FACELIST,LB_GETCURSEL,0,0L);
  80.               if (i == LB_ERR)
  81.                 break;
  82.               SendDlgItemMessage(hDlg, ID_SIZELIST, LB_RESETCONTENT, 0, 0L);
  83.  
  84.               GetFontSizes(hDlg, i);
  85.               for (i = 1;  i < 256;  i++)
  86.               {
  87.                 if (BitMaskTest(abFontSizeBitmask, i))
  88.                 {
  89.                   sprintf(szBuf, "%d", i);
  90.                   SendDlgItemMessage(hDlg, ID_SIZELIST, LB_ADDSTRING, 0,
  91.                                             (LONG) (LPSTR) szBuf);
  92.                 }
  93.               }
  94.               SendDlgItemMessage(hDlg, ID_SIZELIST, LB_SETCURSEL, 0, 0L);
  95.               ShowSampleFont(hDlg, FALSE);
  96.               break;
  97.  
  98.             case LBN_DBLCLK:
  99.               goto do_it;
  100.           }
  101.           break;
  102.  
  103.         case ID_SIZELIST:
  104.           switch (HIWORD(lParam))
  105.           {
  106.             case LBN_SELCHANGE :
  107.               ShowSampleFont(hDlg, FALSE);
  108.               break;
  109.  
  110.             case LBN_DBLCLK :
  111.               goto do_it;
  112.           }
  113.           break;
  114.  
  115.  
  116.         case IDOK :
  117. do_it:
  118.           ShowSampleFont(hDlg, TRUE);
  119.           EndDialog(hDlg, TRUE);
  120.           break;
  121.  
  122.         case IDCANCEL :
  123.           EndDialog(hDlg, FALSE);
  124.           break;
  125.       }
  126.  
  127.       return TRUE;
  128.  
  129.   }
  130.  
  131.   return FALSE;
  132. }
  133.  
  134.  
  135. VOID PASCAL GetTypefaces(HWND hWnd) 
  136.   FARPROC lpEnumFunc; 
  137.   HDC hDC; 
  138.  
  139.   idxTypeface = 0; 
  140.  
  141.   hDC = GetDC(hWnd); 
  142.   lpEnumFunc = MakeProcInstance(EnumFunc, hThisInstance); 
  143.   EnumFonts(hDC, (LPSTR) NULL, lpEnumFunc, (LPSTR) (LONG) ENUMFACES); 
  144.   FreeProcInstance(lpEnumFunc); 
  145.   ReleaseDC(hWnd, hDC); 
  146.  
  147.  
  148. VOID PASCAL GetFontSizes(HWND hWnd, int idxFont) 
  149.   HDC hDC; 
  150.   FARPROC lpEnumFunc; 
  151.  
  152.   memset((char *) abFontSizeBitmask, 0, sizeof(abFontSizeBitmask));
  153.   * (WORD *) abFontSizeBitmask = 256;
  154.  
  155.   hDC = GetDC(hWnd); 
  156.   lpEnumFunc = MakeProcInstance(EnumFunc, hThisInstance); 
  157.   EnumFonts(hDC, FontInfo[idxFont].szTypeface, lpEnumFunc, (LPSTR) (LONG) ENUMSIZES); 
  158.   FreeProcInstance(lpEnumFunc); 
  159.   ReleaseDC(hWnd, hDC); 
  160.  
  161.  
  162. int FAR PASCAL EnumFunc(LPLOGFONT lpLogFont, LPTEXTMETRIC lpTextMetric, 
  163.                 short FontType, LPSTR lpData) 
  164.   switch ((int) lpData) 
  165.   { 
  166.     case ENUMFACES : 
  167.       if (idxTypeface >= MAXFONT) 
  168.         return 0; 
  169.       lstrcpy(FontInfo[idxTypeface].szTypeface, lpLogFont->lfFaceName); 
  170.       FontInfo[idxTypeface].lfPitchAndFamily = lpLogFont->lfPitchAndFamily; 
  171.       FontInfo[idxTypeface].lfCharSet = lpLogFont->lfCharSet; 
  172.       return ++idxTypeface; 
  173.  
  174.     case ENUMSIZES : 
  175.       BitMaskSet(abFontSizeBitmask, lpLogFont->lfHeight, TRUE); 
  176.       return TRUE; 
  177.   } 
  178.  
  179.  
  180. VOID PASCAL ShowSampleFont(HWND hDlg, BOOL bPermanent)
  181. {
  182.   HFONT hFont, hOldFont;
  183.   HWND hWnd;
  184.   HDC  hDC;
  185.   RECT r;
  186.   int  iSel, iSize;
  187.   char szBuf[80];
  188.  
  189.  
  190.   iSel = (int) SendDlgItemMessage(hDlg,ID_SIZELIST,LB_GETCURSEL,0,0L);
  191.   SendDlgItemMessage(hDlg, ID_SIZELIST, LB_GETTEXT, iSel,
  192.                            (LONG)(LPSTR) szBuf);
  193.   iSize = atoi(szBuf);
  194.  
  195.   iSel = (int) SendDlgItemMessage(hDlg,ID_FACELIST,LB_GETCURSEL,0,0L);
  196.   SendDlgItemMessage(hDlg, ID_FACELIST, LB_GETTEXT, iSel,
  197.                      (LONG)(LPSTR) szBuf);
  198.  
  199.  
  200.   hWnd = GetDlgItem(hDlg, ID_TESTFONT);
  201.   hDC = GetDC(hWnd);
  202.   GetClientRect(hWnd, (LPRECT) &r);
  203.   FillRect(hDC, (LPRECT) &r, (HBRUSH) GetStockObject(WHITE_BRUSH));
  204.  
  205.   hFont = CreateFont(iSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
  206.                      FontInfo[iSel].lfCharSet,
  207.                      OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  208.                      DEFAULT_QUALITY, FontInfo[iSel].lfPitchAndFamily,
  209.                      szBuf);
  210.   if (hFont)
  211.   {
  212.     hOldFont = SelectObject(hDC, hFont);
  213.     TextOut(hDC, 0, 0, "ABCabc", 6);
  214.     SelectObject(hDC, hOldFont);
  215.     if (bPermanent)
  216.     {
  217.       LPSTOCKINFO lpStockInfo;
  218.       if ((lpStockInfo = (LPSTOCKINFO) GlobalLock(hCurrStockInfo)) != NULL)
  219.       {
  220.         if (lpStockInfo->graphAttrs.hFont != 0)
  221.           DeleteObject(lpStockInfo->graphAttrs.hFont);
  222.         lpStockInfo->graphAttrs.hFont = hFont;
  223.         InvalidateRect(lpStockInfo->hWnd, (LPRECT) NULL, FALSE);
  224.         GlobalUnlock(hCurrStockInfo);
  225.       }
  226.     }
  227.     else
  228.       DeleteObject(hFont);
  229.   }
  230.  
  231.   ReleaseDC(hWnd, hDC);
  232. }
  233.  
  234.  
  235. /*****************************************************************************/
  236. /*                                                                           */
  237. /*                  ROUTINES FOR MANIPULATING BITMASKS                     */
  238. /*                                                                           */
  239. /*****************************************************************************/
  240.  
  241. BitMaskSet(map, c, val)
  242.   BYTE *map;
  243.   WORD c, val;
  244. {
  245.   /* 'c' takes on the values 1..n; since the bitmap numbers
  246.      the bits from 0..n-1, we need to subtract 1 from 'c' before we start.
  247.   */
  248.   if (c >= * (WORD *) map)      /* test if there are c bits in the map */
  249.     return 0;
  250.   map += sizeof(WORD);        /* get past the size indicator       */
  251.   if (val)
  252.     map[c >> 3] |=   1 << (c & 0x07);      /* turn it on  */
  253.   else
  254.     map[c >> 3] &= ~(1 << (c & 0x07));    /* turn it off */
  255.   return 1;
  256. }
  257.  
  258. BitMaskTest(map, c)
  259.   BYTE *map;
  260.   WORD c;
  261. {
  262.   /* 'c' takes on the values 1..n; since the bitmap numbers
  263.      the bits from 0..n-1, we need to subtract 1 from 'c' before we start.
  264.   */
  265.   if (c >= * (WORD *) map)         /* make sure we're inbounds */
  266.     return 0;
  267.   map += sizeof(WORD);               /* get past size indicator  */
  268.   return( map[c >> 3] & (1 << (c & 0x07)) );   /* extract the desired bit  */
  269. }
  270.